package dao;

import common.Communication;
import common.User;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class ChatRecords {
    //群聊聊天记录
    public ArrayList<Communication> groupchatRecords(Communication u) throws ClassNotFoundException, SQLException, IOException {
        Connection conn=null;
        ResultSet res=null;
        ArrayList<Communication>communications=new ArrayList<>();
        Properties info=new Properties();
        info.load(new FileInputStream("src\\sql.properties"));
        String user=info.getProperty("user");
        String password=info.getProperty("password");
        String Driver=info.getProperty("Driver");
        String url=info.getProperty("url");
        //注册驱动
        Class.forName(Driver);
        //获取连接
        conn= DriverManager.getConnection(url,user,password);
        String sql="select * from groupchat where groupid = ? ";
        PreparedStatement presta = conn.prepareStatement(sql);
        presta.setString(1, u.getGroup().getGroupid());
        res=presta.executeQuery();
        while(res.next()){
            Communication communication=new Communication();
            User sender=new User();
            sender.setId(res.getString("sender"));
            communication.setSender(sender);
            communication.setGroup(u.getGroup());
            communication.setContent(res.getString("content"));
            communication.setType(res.getInt("category"));
            communication.setSendDateTime(res.getString("sendtime"));
            communications.add(communication);
        }
        return communications;
    }

    //个人聊天记录
    public ArrayList<Communication> chatRecords(Communication u) throws ClassNotFoundException, SQLException, IOException {
        Connection conn=null;
        ResultSet res=null;
        ArrayList<Communication>communications=new ArrayList<>();
        Properties info=new Properties();
        info.load(new FileInputStream("src\\sql.properties"));
        String user=info.getProperty("user");
        String password=info.getProperty("password");
        String Driver=info.getProperty("Driver");
        String url=info.getProperty("url");
        //注册驱动
        Class.forName(Driver);
        //获取连接
        conn= DriverManager.getConnection(url,user,password);
        String sql="select * from chat where sender = ? and receiver = ?";
        PreparedStatement presta = conn.prepareStatement(sql);
        presta.setString(1, u.getSender().getId());
        presta.setString(2, u.getReceiver().getId());
        res=presta.executeQuery();
        while(res.next()){
            Communication communication=new Communication();
            communication.setSender(u.getSender());
            communication.setReceiver(u.getReceiver());
            communication.setContent(res.getString("content"));
            communication.setType(res.getInt("category"));
            communication.setSendDateTime(res.getString("sendtime"));
            communications.add(communication);
        }
        presta.setString(1,u.getReceiver().getId());
        presta.setString(2, u.getSender().getId());
        res=presta.executeQuery();
        while(res.next()){
            Communication communication=new Communication();
            communication.setSender(u.getReceiver());
            communication.setReceiver(u.getSender());
            communication.setContent(res.getString("content"));
            communication.setType(res.getInt("category"));
            communication.setSendDateTime(res.getString("sendtime"));
            communications.add(communication);
        }
        return communications;
    }
}
